package com.example.tuike.mapper;

import com.example.tuike.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/*** @Author supersaiya
 * @Description TODO

 * @Date 2025/1/28 16:29

 * @Version 1.0*/


@Mapper
public interface UserMapper {
    // 插入用户（返回自增ID）
    @Insert("INSERT INTO Users(username, passwordHash, email, isDeleted, createdAt, updatedAt) " +
            "VALUES(#{username}, #{passwordHash}, #{email}, #{isDeleted}, #{createdAt}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    int insert(User user);

    @Select("SELECT * FROM Users")
    List<User> getUserList();
    // 根据ID查询（排除已删除用户）
    @Select("SELECT * FROM Users WHERE userId = #{userId} AND isDeleted = 0")
    User findById(Integer userId);

    // 根据用户名查询
    @Select("SELECT * FROM Users WHERE username = #{username}")
    User findByUsername(String username);

    // 更新用户信息
    @Update("UPDATE Users SET " +
            "email = #{email}, " +
            "lastLoginAt = #{lastLoginAt}, " +
            "updatedAt = NOW() " +
            "WHERE userId = #{userId}")
    int update(User user);

    // 软删除
    @Update("UPDATE Users SET isDeleted = true WHERE userId = #{userId}")
    int softDelete(Integer userId);
}

